﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using HRM.DAL.DAO;

namespace HRM.DBGatway
{
    public class PunishedProcedureGatway:DbGateway
    {
        public bool HasThisProcedureExists(PunishedProcedure procedure)
        {
            try
            {
                ConnectionObj.Open();
                CommandObj.CommandText = @"select Punished from tblPunished where Punished='"+procedure.Procedure+"'";
                SqlDataReader reader = CommandObj.ExecuteReader();
                while (reader.Read())
                {
                    return true;
                }

            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                ConnectionObj.Close();
            }
            return false;
        }

        public bool SaveProcedure(PunishedProcedure procedure)
        {
            try
            {
                ConnectionObj.Open();
                CommandObj.CommandText = string.Format("insert into tblPunished values(@punished)");
                CommandObj.Parameters.Clear();
                CommandObj.Parameters.Add("@punished", SqlDbType.VarChar).Value = procedure.Procedure;
                CommandObj.ExecuteNonQuery();
                return true;
            }
            catch (Exception)
            {
                throw new Exception("Error ocured while savivg Data");
            }
            finally
            {
                ConnectionObj.Close();
            }
        }

        public List<PunishedProcedure> GetAllProcedure()
        {
            try
            {
                List<PunishedProcedure>procedures=new List<PunishedProcedure>();
                ConnectionObj.Open();
                CommandObj.CommandText = @"select * from tblPunished order by Punished";
                SqlDataReader reader = CommandObj.ExecuteReader();
                while (reader.Read())
                {
                    PunishedProcedure procedure=new PunishedProcedure();
                    procedure.Id = Convert.ToInt32(reader[0]);
                    procedure.Procedure = reader[1].ToString();
                    procedures.Add(procedure);
                }
                return procedures;
            }
            catch (Exception)
            {
                throw new Exception("Error occured during reading Data");
            }
            finally
            {
                ConnectionObj.Close();
            }
        }
    }
}